package com.travel.Controller.User;

import com.github.pagehelper.PageHelper;
import com.travel.Bean.Culture;
import com.travel.Service.Impl.CultureServiceImpl;
import com.travel.util.ConsoleOutputControl;
import com.travel.util.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/UserCulture")
public class UserCultureController {

    @Autowired
    private CultureServiceImpl cultureService;

    @ApiOperation("用户——景区列表，返回list类型，包含的有效数据是{id,spot_name,spot_opentime}," +
            "分别是唯一id，景区名称，景区开放时间；" +
            "进入景区需要使用唯一id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "spot_name",value = "景区名，可以模糊匹配，可以做搜索用"),
            @ApiImplicitParam(name = "page",value = "如果是显示一个页面可以设置当给所在的页码，那么而同时就也要设置limit参数"),
            @ApiImplicitParam(name = "limit",value = "设置当前页码显示的景区条数,单独设置可以限制返回的内容条数")
    })
    @ApiResponses({
            @ApiResponse(code=0,message = "请求成功")
    })
    @PostMapping("/userCultureList")
    public R userCultureList(String spot_name, @RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "15") Integer limit){
        List<Culture> cultures = new ArrayList<>();
        if(page == null && limit == null){
            PageHelper.startPage(1,15);
        } else if (page == null ) {
            PageHelper.startPage(1,limit);
        }else {
            PageHelper.startPage(page,limit);
        }
        cultures = cultureService.getCultureNameList(spot_name);

        return R.ok("0",cultures, (long) cultures.size());
    }


    /**
     * 开放景区
     *
     * @param session   会话
     * @param cultureId 景区id
     * @return {@link R}
     */
    @ApiOperation("获取景区信息、简介")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cultureId",value = "景区id号")
    })
    @PostMapping("/openCulture")
    public R openCulture(HttpSession session,Integer cultureId){
        Culture culture;
        try{
            culture = cultureService.findCultureById(cultureId);
        }catch (Exception e){
            System.out.println(ConsoleOutputControl.getColoredOutputString("获取景区信息错误", 33));
            return R.no(1,"获取景区信息失败");
        }

        return R.ok(0,culture);
    }




}
